# -*- coding:utf-8 -*-

import requests
import json
import time
import random
from pymongo import MongoClient
import re

from utils.HttpUtils import HttpUtils

"""
天天基金-基金排行-概要-公告
"""
class TianTian():
    def __init__(self):
        self.headers = {
            "User-Agent": "okhttp/3.12.0",
            "Content-Type": "application/x-www-form-urlencoded"
        }
        self.deviceid = "cbc5d43197df826cbfbd4cde1173362f%7C%7C037584220931968"
        self.MobileKey = "cbc5d43197df826cbfbd4cde1173362f%7C%7C037584220931968"

    # 基金排名
    def get_contents(self, page=0):
        postData = {
            "RISKLEVEL":"",
            "ISABNORMAL":"false",
            "RLEVEL_ZS":"",
            "ESTABDATE":"",
            "pageSize":"30",
            "pageIndex":page,
            "DISCOUNT":"",
            "plat":"Android",
            "deviceid":self.deviceid,
            "BUY":"true",
            "appType":"ttjj",
            "MobileKey":self.MobileKey,
            "SortColumn":"SYL_Y",
            "version":"6.1.7",
            "CompanyId":"",
            # "gToken":"ceaf-5b157e504693b6b2477d18b1c0c6537a",
            "TOPICAL":"",
            "Sort":"desc",
            "FundType":"0",
            "product":"EFund",
            "ENDNAV":""
        }
        url = "https://fundmobapi.eastmoney.com/FundMNewApi/FundMNRankNewList"
        time.sleep(random.randint(2, 3))
        html = HttpUtils.do_request("POST", url, self.headers, postData)

        data_json = json.loads(html.text)
        total_count = data_json['TotalCount']
        page_no = (total_count // 30) + (total_count % 30)
        for d in data_json['Datas']:
            dict_data = dict()
            dict_data['FCODE'] = d['FCODE']  # 基金代码
            dict_data['SHORTNAME'] = d['SHORTNAME']  # 基金名称
            print("=========提取基金信息【{SHORTNAME}】============".format(SHORTNAME=d['SHORTNAME']))
            self.get_general(d['FCODE'], dict_data)
            self.get_notes(d['FCODE'])
        # 根据总数循环
        if page <= page_no:
            page += 1
            self.get_comments(page)
        print("=========全部提取基金信息完成============")
    # 概况
    def get_general(self, fcode, dict_data):
        postData = {
            "OSVersion":"5.0.2",
            "FCODE":fcode,
            "MobileKey":self.MobileKey,
            "version":"6.1.5",
            "AppVersion":"6.1.7",
            "passportid":"",
            "plat":"Android",
            "deviceid":self.deviceid,
            "product":"EFund"
        }
        url = "https://fundmobapi.eastmoney.com/FundMNewApi/FundMNDetailInformation"
        time.sleep(random.randint(2, 3))
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)
        dict_data['ESTABDATE'] = data_json['Datas']['ESTABDATE']  # 成立时间
        dict_data['BENCH'] = data_json['Datas']['BENCH']  # 业绩比较标准
        dict_data['INVTGT'] = data_json['Datas']['INVTGT']  # 投资目标
        dict_data['INVSTRA'] = data_json['Datas']['INVSTRA']  # 投资策略
        num = 1
        flag = 0
        for s in dict_data['INVSTRA'].split():
            if re.match("\d", s) is not None:
                flag += 1
            if flag >= 2:
                flag = 1
                num += 1
            if flag >0 and flag <= 1:
                if dict_data.get('INVSTRA_' + str(num)) is None:
                    dict_data['INVSTRA_' + str(num)] = s
                else:
                    dict_data['INVSTRA_' + str(num)] += "  " + s
        self.insertItem("contents", dict_data)
        print("=========提取概况============")

    # 公告
    def get_notes(self, fcode, page=1):
        postData = {
            "FCODE":fcode,
            "NEWCATEGORY":"0",
            "cToken":"kd8kheua6cr6-jdaked8eck8cn6re8dc",
            "userId":"uid",
            "serverVersion":"6.1.5",
            "pageIndex":page,
            "plat":"Android",
            "deviceid":self.deviceid,
            "OSVersion":"5.0.2",
            "appVersion":"6.1.7",
            "MobileKey":self.MobileKey,
            "version":"6.1.7",
            "passportid":"1234567890",
            "uToken":"utoken",
            "pagesize":"10",
            "product":"EFund"
        }
        url = "https://fundmobapi.eastmoney.com/FundMNewApi/FundMNNoticeList"
        time.sleep(random.randint(2, 3))
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text)
        total_count = data_json['TotalCount']
        page_no = (total_count // 30) + (total_count % 30)
        for d in data_json['Datas']:
            dict_data = dict()
            dict_data['ID'] = d['ID']
            dict_data['FCODE'] = fcode
            dict_data['TITLE'] = d['TITLE']  # 标题
            dict_data['PUBLISHDATE'] = d['PUBLISHDATE']  # 时间
            type_id = d['NEWCATEGORY']
            if type_id == '1':
                dict_data['TYPE'] = "发行运作"
            elif type_id == '2':
                dict_data['TYPE'] = "分红配送"
            elif type_id == '3':
                dict_data['TYPE'] = "定期报告"
            elif type_id == '4':
                dict_data['TYPE'] = "人事调整"
            elif type_id == '5':
                dict_data['TYPE'] = "基金销售"
            elif type_id == '6':
                dict_data['TYPE'] = "其他公告"
            self.get_notes_detail(fcode, d['ID'], dict_data)
            print("=========提取公告【{TITLE}】============".format(TITLE=d['TITLE']))
        # 根据总共条数循环
        if page <= page_no:
            page += 1
            self.get_notes(fcode, page)

    def get_notes_detail(self, fcode, id, dict_data):
        postData = {
            "deviceid":"app_danganye_f10",
            "version":"V2.1.0",
            "product":"EFund",
            "plat":"Iphone",
            "FCODE":fcode,# "161127",
            "ID":id,#"AN201912051371600824",
            "_":"1575611613928",
            "callback":"Zepto1575611611615"
        }
        url = "https://fundmobapi.eastmoney.com/FundMApi/FundNoticeDetail.ashx"
        time.sleep(random.randint(2, 3))
        html = HttpUtils.do_request("GET", url, self.headers, postData)

        data_json = json.loads(html.text.replace("Zepto1575611611615", "").replace("(", "").replace(")", ""))
        dict_data['CONTEXT'] = data_json['Datas']['CONTEXT']
        print("=========提取公告【{TITLE}】内容============".format(TITLE=data_json['Datas']['TITLE']))
        self.insertItem("notes", dict_data)

    def insertItem(self, tableName, data):
        my_set = db[tableName]
        my_set.insert_one(data)


if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    tiantian = TianTian()
    tiantian.get_contents()